package org.senseframework.support.orm.mybatis.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * Author lanjian
 * Email  jianlan@iflytek.com
 */
public class MultipleDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
    private static final Logger logger = LoggerFactory.getLogger(MultipleDataSource.class);

    public static void setDataSourceKey(String dataSource) {
        if (logger.isInfoEnabled()) {
            logger.info("setDataSourceKey " + dataSource + " 数据库 ");
        }
        dataSourceKey.set(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        String key = dataSourceKey.get();
        if (logger.isInfoEnabled()) {
            logger.info("determineCurrentLookupKey " + key + " 数据库 ");
        }
        return key;
    }
}
